package com.ssmdemo.aop.imp;


import com.ssmdemo.aop.bean.PermissionCode;
import com.ssmdemo.constant.LoginConstant;
import com.ssmdemo.pojo.BusinessException;
import com.ssmdemo.pojo.User;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpSession;
import java.io.UnsupportedEncodingException;
import java.util.Set;

@Aspect
@Component
public class PermissionCodeAop {

    @Autowired
    HttpSession session;

    @Around("@annotation(permissionCode)")
    public Object hanler(ProceedingJoinPoint proceedingJoinPoint,PermissionCode permissionCode) throws UnsupportedEncodingException {

        User user=(User) session.getAttribute(LoginConstant.SUCCESS_USER);

        Set<String> stringSet=user.getPermissionCode();

        String value=permissionCode.value();

        Object result=null;

        if (stringSet.contains(value)){
            try {
                proceedingJoinPoint.proceed(proceedingJoinPoint.getArgs());
            } catch (Throwable throwable) {
                throwable.printStackTrace();
            }
        }else {
            throw new BusinessException(4004,"你没用权限访问该功能");
        }

        return result;

    }
}
